﻿using RAP.Framework.Libary.DomTemplateBuilder.Attributes;
using RAP.Framework.Libary.DomTemplateBuilder.Handler;
using RAP.Framework.Libary.Utils;
using System.IO;

namespace RAP.Framework.Libary.DomTemplateBuilder.Element
{
    /// <summary>
    /// 脚本元素
    /// </summary>
    [TagHandler( typeof( ScriptHandler ) )]
    public class Script : Dynamic
    {
        /// <summary>
        /// 
        /// </summary>
        public string Src
        {
            get
            {
                return this.ToString ( "Src" );
            }
        }

		public bool IsDebug
		{
			get
			{
				return this.ToBoolean ( "IsDebug" );
			}
		}

		private string _scriptContent = null;
		public string ScriptContent
		{
			get
			{
				if ( _scriptContent.IsNullOrEmpty ( ) || this.IsDebug )
				{
					string scriptSrc = this.Src;
					scriptSrc = scriptSrc.MapWWWPath( );
					if ( File.Exists ( scriptSrc ) )
					{
						_scriptContent = File.ReadAllText ( scriptSrc );
					}
					else
					{
						throw new FileNotFoundException ( "指定脚本文件不存在！", scriptSrc );
					}
				}
				return _scriptContent;
			}
		}
    }
}
